package com.hudilao.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hudilao.common.BaseContext;
import com.hudilao.common.R;
import com.hudilao.dto.CustomerDetailDTO;
import com.hudilao.dto.CustomerLoginDTO;
import com.hudilao.entity.Cart;
import com.hudilao.entity.Customer;
import com.hudilao.entity.CustomerMembership;
import com.hudilao.mapper.CustomerMapper;
import com.hudilao.properties.JwtProperties;
import com.hudilao.service.CustomerService;
import com.hudilao.util.JwtUtil;
import com.hudilao.vo.CustomerDetailVO;
import com.hudilao.vo.CustomerLoginVO;
import com.hudilao.common.constant.JwtClaimsConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.transform.Result;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("user/user")  //用户端/客户模块
//@Api(tags = "用户相关接口")
@Slf4j
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private CustomerMapper customerMapper;



    /**
     * 微信用户登录
     * @param request
     * @param customerLoginDTO
     * @return
     */
    @PostMapping("/login")
    private R<CustomerLoginVO> login(HttpServletRequest request,@RequestBody CustomerLoginDTO customerLoginDTO){

        log.info("微信用户登录：{}",customerLoginDTO.getCode());

        //微信登录
        Customer customer = customerService.wechatLogin(customerLoginDTO);

        //为微信用户生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID,customer.getId());
        String token = JwtUtil.createJWT(jwtProperties.getCustomerSecretKey(), jwtProperties.getCustomerTtl(),claims);

        CustomerLoginVO customerLoginVO = CustomerLoginVO.builder()
                .customerID(customer.getId())
                .openid(customer.getOpenid())
                .token(token)
                .build();

        // 检查是否已有会话
        HttpSession session = request.getSession(false);
        if (session == null) {
            session = request.getSession(); // 创建新会话
        }
        //登录后设置session状态
        session.setAttribute("customer",customerLoginVO.getCustomerID());


        return R.success(customerLoginVO);
    }

    /**
     * 用户修改个人信息
     * @param customerDetailDTO
     * @return
     */
    @PostMapping("/update")
    public R<String> update(@RequestBody CustomerDetailDTO customerDetailDTO){

        //接收传来的信息，包括用户性别、手机号、生日
        log.info("个人信息：{}",customerDetailDTO);

        //判断客户id
        Long customerId = 5L;//BaseContext.getCurrentId();

        //判断是否有修改？

        //更新customer表
        customerMapper.updateCustomerInfo(customerId,customerDetailDTO.getGender(),customerDetailDTO.getPhoneNumber(),customerDetailDTO.getBirthdate());

        return R.success("修改成功");
    }


}
